package com.smedi.ismedi.inmail.core.application.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.smedi.ismedi.inmail.core.application.command.*;
import com.smedi.ismedi.inmail.core.application.convert.AppConverter;
import com.smedi.ismedi.inmail.core.application.query.AppVersionQueryService;
import com.smedi.ismedi.inmail.core.application.service.AppVersionApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.AppVersion;
import com.smedi.ismedi.inmail.core.domain.service.AppVersionDomainService;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.smedi.ismedi.inmail.core.infrastructure.constant.AppVersionErrorEnum.VERSION_EXIST;

/**
 * @className: AppVersionApplicationServiceImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2023/8/30 10:22
 */

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class AppVersionApplicationServiceImpl implements AppVersionApplicationService {

    private final AppConverter appConverter;

    private final AppVersionDomainService appVersionDomainService;

    private final AppVersionQueryService appVersionQueryService;

    @Override
    public Long create(AppVersionCreateCommand appVersionCreateCommand) {
        AppVersion temp = appVersionQueryService.get(AppVersionQuery
                .builder()
                .appVersion(appVersionCreateCommand.getAppVersion())
                .systemType(appVersionCreateCommand.getSystemType())
                .build());
        if (temp != null) {
            throw LogicException.raise(VERSION_EXIST);
        }
        AppVersion appVersion = appConverter.appVersionCreateCommandToAppVersion(appVersionCreateCommand);
        return appVersionDomainService.create(appVersion);
    }

    @Override
    public Long update(AppVersionUpdateCommand appVersionUpdateCommand) {
        AppVersion temp = appVersionQueryService.get(AppVersionQuery
                .builder()
                .appVersion(appVersionUpdateCommand.getAppVersion())
                .systemType(appVersionUpdateCommand.getSystemType())
                .idNe(appVersionUpdateCommand.getId())
                .build());
        if (temp != null) {
            throw LogicException.raise(VERSION_EXIST);
        }
        AppVersion appVersion = appConverter.appVersionUpdateCommandToAppVersion(appVersionUpdateCommand);
        String[] versions = StringUtils.split(appVersion.getAppVersion(), ".");
        appVersion.setVersionOne(versions[0]);
        appVersion.setVersionTwo(versions[1]);
        appVersion.setVersionThree(versions[2]);
        return appVersionDomainService.update(appVersion);
    }

    @Override
    public void remove(AppVersionRemoveCommand appVersionRemoveCommand) {
        AppVersionQuery appVersionQuery = appConverter.appVersionRemoveCommandToAppVersionQuery(appVersionRemoveCommand);
        List<Long> ids = new ArrayList<>();
        if (appVersionQuery.getId() != null) {
            ids.add(appVersionQuery.getId());
        }
        if (CollectionUtils.isNotEmpty(appVersionQuery.getIds())) {
            ids.addAll(appVersionQuery.getIds());
        }
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        appVersionDomainService.remove(appVersionQuery);
    }

    @Override
    public void enable(AppVersionEnableCommand appVersionEnableCommand) {
        appVersionDomainService.update(AppVersion.builder().id(appVersionEnableCommand.getId()).enable(true).build());
    }

    @Override
    public void disable(AppVersionDisableCommand appVersionDisableCommand) {
        appVersionDomainService.update(AppVersion.builder().id(appVersionDisableCommand.getId()).enable(false).build());
    }
}